#!/usr/bin/env python  
# encoding: utf-8   

""" 
@version: v1.0 
@author: null 
@software: PyCharm 
@file: main.py
@time: 2017.3.12 15:54 
"""

import requests
import bs4
import time
from multiprocessing import Pool
import os

root_url='http://wufazhuce.com/'

def get_url(num):
    return root_url+'one/'+str(num)
    pass

def get_urls(num):
    index = range(500,num)[::-1]
    urls = map(get_url,index)
    return urls
    pass

def get_data(url):
    print(url)
    try:
        response = requests.get(url)
    except requests.exceptions.ConnectTimeout:
        print("连接超时")
        return
    except requests.exceptions.Timeout:
        print("连接超时")
        return
    except:
        print(url+"连接错误")
    if(response.status_code!=200):
        print(url+"请求失败...")
        return
    soup=bs4.BeautifulSoup(response.text,'html.parser')

    day= soup.find('p',class_='dom')
    may= soup.find('p',class_='may')
    oneCita= soup.find('div',class_='one-cita')
    oneImg=soup.find('div',class_='one-imagen')
    imgUrl=oneImg.img.attrs['src']
    img = requests.get(imgUrl)
    date=may.string.split(' ')
    content= oneCita.string.strip()
    folder= create_folder(date,day.string)
    open("./"+folder+"/每日一文.txt","w",encoding='utf-8').write(content)
    if img.status_code == 200:
        open("./"+folder+"/每日一图.jpg",'wb').write(img.content)
    else:
        print(url+"请求图片失败...")
    time.sleep(5)
    pass

def create_folder(pathArray,day):
    if not os.path.exists(pathArray[1]):
        os.mkdir(pathArray[1])
    secondFolder= pathArray[1]+"/"+pathArray[0]
    if not os.path.exists(secondFolder):
        os.mkdir(secondFolder)
    thridFolder= pathArray[1]+"/"+pathArray[0]+"/"+day
    if not os.path.exists(thridFolder):
        os.mkdir(thridFolder)
    return thridFolder
    pass

if __name__ == "__main__":
    pool = Pool(10)
    urls = get_urls(882)
    start = time.time()
    datalist = pool.map(get_data,urls)
    pool.close()
    pool.join()
    end = time.time()
    print('时间: %.2f s' % (end - start))
    pass  
